from models.models import Feedback
from django.http.response import JsonResponse
from models.models.RequestResult import RequestResult as Res
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache


@csrf_exempt
def submit_feedback(request):
    user = request.session['user']
    backText = request.POST.get("backText")
    feedback = Feedback()
    feedback.back_user_id = user.get("id")
    feedback.back_text = backText
    feedback.save()
    return JsonResponse(Res.success(None))


@csrf_exempt
def get_feedback(request):
    user = request.session['user']
    feedbacks = Feedback.objects.filter(back_user_id=user["id"])
    print(feedbacks)
    res_feedbacks = list()
    for f in feedbacks:
        res_f = dict()
        res_f["id"] = f.id
        res_f["back_text"] = f.back_text
        res_f["reply"] = f.reply
        res_feedbacks.append(res_f)
    return JsonResponse(Res.success(res_feedbacks))

